/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itesm.tps.proyecto.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import mx.edu.itesm.tps.proyecto.pojo.Alumno;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.webflow.action.FormAction;

public class AlumnoDao extends FormAction implements ProjectEventDao {

    private SimpleJdbcTemplate simpleJdbcTemplate;
    private SimpleJdbcInsert insertAlumno;

    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
        this.insertAlumno = new SimpleJdbcInsert(dataSource).withTableName("alumno");
    }
    
   public List getCursosAlumno(String id) {        
        return simpleJdbcTemplate.queryForList("select * from courses where id in (select curso from alumno where nombre = ?)", new Object[]{id});
    }
    
    private Alumno getByMatricula(String id) {
        try {
            return simpleJdbcTemplate.queryForObject("SELECT * FROM users WHERE user = ?", new AlumnoMapper(), id);
        } catch (EmptyResultDataAccessException erdae) {
            Alumno u = new Alumno();
            u.setMatricula(id);

            if ((id.startsWith("A") || (id.startsWith("a")))) {
                u.setTipo(Alumno.TYPE_ALUM);
            } 

            add(u);

            return u;
        }
    }
    
    public void addAlumnoToCourse(String alumnoId, String name,String curso) {
        if (alumnoId != null && name != null && !alumnoId.equals("") && !name.equals("")) {
            
                Alumno a = new Alumno();
                a.setMatricula(alumnoId);
                a.setNombre(name);
                a.setCurso(curso);
                a.setTipo(Alumno.TYPE_ALUM);                

                add(a);
            
        }
    }

    public void add(Alumno alum) {
        Map<String, Object> parameters = new HashMap<String, Object>(Alumno.class.getDeclaredFields().length);
        parameters.put("matricula", alum.getMatricula());
        parameters.put("nombre", alum.getNombre());
        parameters.put("curso", alum.getCurso());
        parameters.put("tipo", alum.getTipo());

        insertAlumno.execute(parameters);
    }

    private class AlumnoMapper implements ParameterizedRowMapper<Alumno> {

        public Alumno mapRow(ResultSet rs, int rowNum) throws SQLException {
            Alumno alum = new Alumno();
            alum.setMatricula(rs.getString("matricula"));
            alum.setNombre(rs.getString("nombre"));
            alum.setNombre(rs.getString("curso"));
            alum.setTipo(rs.getInt("tipo"));
            return alum;
        }
    }
}